package com.applications.codemash.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.sax.Element;
import android.sax.EndTextElementListener;

public class Speaker implements Comparable<Speaker>, Serializable
{
	private static final long serialVersionUID = -6837232565486488786L;
	
	private String _name;
	private String _biography;
	private List<String> _sessions;
	private String _twitter;
	private String _blog;
	private String _uri;
	private String _imageUrl;
	
	public Speaker(Element speakerElement)
	{
		speakerElement.getChild(Constants.NAME_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				setName(body);
			}
		});
		
		speakerElement.getChild(Constants.BIOGRAPHY_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				setBiography(body);
			}
		});
		
		speakerElement.getChild(Constants.SESSION_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				getSessions().clear();
			}
		});
		
		speakerElement.getChild(Constants.SESSION_URI_INNER_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				getSessions().add(body);
			}
		});
		
		speakerElement.getChild(Constants.TWITTER_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				setTwitterHandle(body);
			}
		});
		
		speakerElement.getChild(Constants.BLOG_URL_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				setBlogUrl(body);
			}
		});
		
		speakerElement.getChild(Constants.SPEAKER_URI_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				if (!body.startsWith("/"))
					body = "/" + body;
				
				setSpeakerUri(body);
			}
		});
	}
	
	private Speaker()
	{
		/* empty constructor */
	}
	
	/* Obligatory get/set methods */
	public String getName() { return _name; }
	public String getBiography() { return _biography; }
	public List<String> getSessions()
	{
		if (_sessions == null)
			_sessions = new ArrayList<String>();
		
		return _sessions;
	}
	public String getTwitterHandle() { return _twitter; }
	public String getBlogUrl()
	{
		if (_blog == "")
			return "No Blog";
		return _blog;
	}
	public String getSpeakerUri() { return _uri; }
	public String getImageUrl()
	{
		if (_imageUrl == null || _imageUrl == "")
			_imageUrl = PerformImageUrlLookupForSpaeker();
		
		return _imageUrl;
	}
	
	private String PerformImageUrlLookupForSpaeker()
	{
		if (this.getTwitterHandle().length() == 0)
			return "";
		
		String twitterUserUrl = Constants.GetTwitterShowUrlString(this.getTwitterHandle());
		return TwitterLookup.GetImageUrlForTwitterUserUrl(twitterUserUrl);
	}

	protected void setName(String name) { _name = name; }
	protected void setBiography(String biography) { _biography = biography; }
	protected void setTwitterHandle(String twitter) { _twitter = twitter; }
	protected void setBlogUrl(String blogUrl) { _blog = blogUrl; }
	protected void setSpeakerUri(String uri) { _uri = uri; }
	
	public Speaker copy()
	{
		Speaker speaker = new Speaker();
		speaker.setName(getName());
		speaker.setBiography(getBiography());
		speaker.setTwitterHandle(getTwitterHandle());
		speaker.setBlogUrl(getBlogUrl());
		speaker.setSpeakerUri(getSpeakerUri());
		
		for(int i=0; i<getSessions().size(); i++)
		{
			speaker.getSessions().add(getSessions().get(i));
		}
		
		return speaker;
	}

	@Override
	public int compareTo(Speaker another)
	{
		return getName().compareTo(another.getName());
	}
}
